package com.wt.admin.controller.sys;


import com.aizuda.easy.security.domain.Rep;
import com.wt.admin.config.aspect.annotation.LogAno;
import com.wt.admin.domain.dto.sys.DataPermissionsTablesDTO;
import com.wt.admin.domain.entity.sys.SysDataPermissionsEntity;
import com.wt.admin.domain.entity.sys.SysRoleEntity;
import com.wt.admin.domain.vo.DataPermissionsFieldsVO;
import com.wt.admin.domain.vo.DataPermissionsFindByTablesVO;
import com.wt.admin.domain.vo.DataPermissionsTablesVO;
import com.wt.admin.service.sys.SysDataPermissionsService;
import com.wt.admin.service.sys.SysRoleService;
import com.wt.admin.util.PageUtil;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("dataPermissions")
public class SysDataPermissionsController {

    @Resource
    private SysDataPermissionsService sysDataPermissionsService;
    @Resource
    private SysRoleService sysRoleService;

    @LogAno(name = "数据权限列表")
    @PostMapping("findDataPermissions")
    public Rep<PageUtil.PageVO<SysDataPermissionsEntity>> findDataPermissions(@RequestBody PageUtil.PageDTO<SysDataPermissionsEntity> data){
        return Rep.ok(sysDataPermissionsService.find(data));
    }

    @LogAno(name = "编辑数据权限")
    @PostMapping("editDataPermissions")
    public Rep<?> editDataPermissions(@RequestBody SysDataPermissionsEntity data){
        sysDataPermissionsService.edit(data);
        return Rep.ok();
    }

    @LogAno(name = "删除数据权限")
    @PostMapping("delDataPermissions")
    public Rep<?> delDataPermissions(@RequestBody SysDataPermissionsEntity data){
        sysDataPermissionsService.del(data);
        return Rep.ok();
    }

    @LogAno(name = "查询数据库所有表")
    @PostMapping("findByTables")
    public Rep<DataPermissionsFindByTablesVO> findByTables(@RequestBody SysDataPermissionsEntity data){
        List<SysRoleEntity> all = sysRoleService.findAll();
        List<DataPermissionsTablesVO> byTables = sysDataPermissionsService.findByTables(data);
        return Rep.ok(new DataPermissionsFindByTablesVO(byTables,all));
    }

    @LogAno(name = "查询表的所有列")
    @PostMapping("findByFields")
    public Rep<List<DataPermissionsFieldsVO>> findByFields(@RequestBody SysDataPermissionsEntity data){
        return Rep.ok(sysDataPermissionsService.findByFields(data));
    }

    @LogAno(name = "查询列的所有值")
    @PostMapping("findByFieldValue")
    public Rep<List<Object>> findByFieldValue(@RequestBody DataPermissionsTablesDTO data){
        return Rep.ok(sysDataPermissionsService.findByFieldValue(data));
    }

}
